package com.hzlj.position.locate.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.redis.annotation.SyncLock;
import com.google.common.collect.Lists;
import com.hzlj.position.locate.common.dto.reportSync.ReportSyncQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportSync.ReportSyncQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportSync.ReportSyncUpsertDTO;
import com.hzlj.position.locate.common.model.ReportSync;
import com.hzlj.position.locate.dao.ReportSyncDao;
import com.hzlj.position.locate.service.ReportSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 通信联络核查-同步配置表(ReportSync)表服务接口
 *
 * @author lifh
 * @date 2023-03-13 19:10:15
 */
@Slf4j
@Service
public class ReportSyncServiceImpl implements ReportSyncService {
    @Resource
    private ReportSyncDao reportSyncDao;


    @Override
    public void upsert(String jgCode, String syncType, Date syncDay) {
        jgCode = ObjectUtil.defaultIfEmpty(jgCode, "1");
        ReportSyncUpsertDTO upsert = new ReportSyncUpsertDTO();
        upsert.setJgCode(jgCode);
        upsert.setSyncType(syncType);
        upsert.setLatestDay(DateUtil.beginOfDay(syncDay));

        upsert(
                Lists.newArrayList(upsert)
        );
    }


    @Override
    @SyncLock
    public void upsert(Collection<ReportSyncUpsertDTO> dtos) {
        if (ObjectUtil.isEmpty(dtos)) {
            return;
        }

        List<ReportSync> syncs = dtos.stream()
                .map(e -> {
                    ReportSync reportSync = new ReportSync();
                    reportSync.setJgCode(e.getJgCode());
                    reportSync.setSyncType(e.getSyncType());
                    reportSync.setLatestDay(DateUtil.beginOfDay(e.getLatestDay()));
                    return reportSync;
                }).collect(Collectors.toList());

        for (ReportSync sync : syncs) {
            List<ReportSyncQueryResultDTO> exists
                    = this.reportSyncDao.listReportSync(new ReportSyncQueryParamDTO().setJgCode(sync.getJgCode()).setSyncType(sync.getSyncType()));
            //更新
            if (ObjectUtil.isNotEmpty(exists)) {
                this.reportSyncDao.updateReportSync(sync);
                continue;
            }
            this.reportSyncDao.save(sync);
        }
    }


    /**
     * 检测是否同步
     */
    @Override
    public boolean synced(String jgCode, String syncType, Date syncDay) {
        jgCode = ObjectUtil.defaultIfEmpty(jgCode, "1");
        Date latestDay = this.getLatestDay(jgCode, syncType);
        if (latestDay != null
                && DateUtil.beginOfDay(latestDay).compareTo(DateUtil.beginOfDay(syncDay)) >= 0) {
            log.warn("当天的同步已经处理，不需要重复处理：jgCode={},syncType={},latestDay={}", jgCode, syncType, syncDay);
            return true;
        }
        return false;
    }


    /**
     * jgCode-latestDay
     */
    @Override
    public Map<String, Date> mapReportSync(String syncType) {
        return reportSyncDao.listReportSync(new ReportSyncQueryParamDTO().setSyncType(syncType))
                .stream().collect(Collectors.toMap(ReportSyncQueryResultDTO::getJgCode, ReportSyncQueryResultDTO::getLatestDay));
    }

    @Override
    public Date getLatestDay(String jgCode, String syncType) {
        List<ReportSyncQueryResultDTO> syncs = reportSyncDao.listReportSync(new ReportSyncQueryParamDTO().setSyncType(syncType).setJgCode(jgCode));
        if (ObjectUtil.isEmpty(syncs)) {
            return null;
        }
        return syncs.get(0).getLatestDay();
    }
}
